package com.cavytech.wear;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;

public class DeviceListAdapter extends BaseAdapter {

    public final static int LISTTYPE_DISCOVERED = 0;
    public final static int LISTTYPE_PAIRED = 1;
    public final static int LISTTYPE_CONNECTED = 2;

    private final static String KEY_DEV_NAME = "DeviceName";
    private final static String KEY_DEV_ADDR = "DeviceAddress";
    private final static String KEY_DEV_LISTENER1 = "DeviceClickListener1";
    private final static String KEY_DEV_LISTENER2 = "DeviceClickListener2";

    private LayoutInflater mAdapterLayoutInflater;
    private ArrayList<HashMap<String,Object>> mDeviceArrayList;
    private int mListType;

    public DeviceListAdapter(Context c, int type) {

        mAdapterLayoutInflater = LayoutInflater.from(c);
        mDeviceArrayList = new ArrayList<HashMap<String,Object>>();
        mListType = type;
    }

    private Object getObj( int position, String key ) {
        return mDeviceArrayList.get( position ).get( key );
    }

    public void addItem( String name, String address, View.OnClickListener listener1, View.OnClickListener listener2 ) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        for ( int idx = 0; idx < mDeviceArrayList.size(); idx++ ) {
            if ( getObj( idx, KEY_DEV_ADDR ).equals( address ) ) {
                return;
            }
        }
        map.put( KEY_DEV_NAME, name );
        map.put( KEY_DEV_ADDR, address );
        map.put( KEY_DEV_LISTENER1, listener1 );
        map.put( KEY_DEV_LISTENER2, listener2 );
        mDeviceArrayList.add( map );

        this.notifyDataSetChanged();
    }

    public void removeItem( String address ) {
        if ( !mDeviceArrayList.isEmpty() ) {
            for ( int idx = 0; idx < mDeviceArrayList.size(); idx++ ) {
                if ( getObj( idx, KEY_DEV_ADDR ).equals( address ) ) {
                    mDeviceArrayList.remove( idx );
                    this.notifyDataSetChanged();
                    break;
                }
            }
        }
    }

    public boolean isItemExist( String address ) {
        if ( !mDeviceArrayList.isEmpty() ) {
            for ( int idx = 0; idx < mDeviceArrayList.size(); idx++ ) {
                if ( getObj( idx, KEY_DEV_ADDR ).equals( address ) ) {
                    return true;
                }
            }
        }
        return false;
    }

    public void removeAllItem() {
        while ( !mDeviceArrayList.isEmpty() ) {
            mDeviceArrayList.remove( 0 );
        }
        this.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return mDeviceArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ItemView item;

        if ( convertView == null ) {
            switch( mListType ) {
                case LISTTYPE_PAIRED:
                case LISTTYPE_CONNECTED:
                    convertView = mAdapterLayoutInflater.inflate(R.layout.device_list_item_2, null);
                    item = new ItemView(
                            ( TextView )convertView.findViewById( R.id.DeviceName ),
                            ( Button )convertView.findViewById( R.id.DeviceConnect ),
                            ( Button )convertView.findViewById( R.id.DeviceUnpair ) );
                break;

                default:
                    convertView = mAdapterLayoutInflater.inflate(R.layout.device_list_item_1, null);
                    item = new ItemView(
                            ( TextView )convertView.findViewById( R.id.DeviceName ),
                            ( Button )convertView.findViewById( R.id.DeviceAction ) );
                break;
            }

            convertView.setTag( item );
        } else {
            item = ( ItemView )convertView.getTag();
        }

        item.mTextView.setText( mDeviceArrayList.get( position ).get( "DeviceName" ) + "\n" + mDeviceArrayList.get( position ).get( "DeviceAddress" ) );
        switch ( mListType ) {
            case LISTTYPE_DISCOVERED:
                item.mButton1.setText( R.string.Pair );
                item.mButton1.setOnClickListener( ( View.OnClickListener )getObj( position, KEY_DEV_LISTENER1 ) );
                break;

            case LISTTYPE_PAIRED:
                item.mButton1.setText( R.string.Connect );
                item.mButton1.setOnClickListener( ( View.OnClickListener )getObj( position, KEY_DEV_LISTENER1 ) );
                item.mButton2.setText( R.string.Unpair );
                item.mButton2.setOnClickListener( ( View.OnClickListener )getObj( position, KEY_DEV_LISTENER2 ) );
                break;

            case LISTTYPE_CONNECTED:
                item.mButton1.setText( R.string.State );
                item.mButton1.setOnClickListener( ( View.OnClickListener )getObj( position, KEY_DEV_LISTENER1 ) );
                item.mButton2.setText( R.string.Disconnect );
                item.mButton2.setOnClickListener( ( View.OnClickListener )getObj( position, KEY_DEV_LISTENER2 ) );
                break;
        }

        return convertView;
    }

    protected class ItemView {
        public TextView mTextView;
        public Button mButton1;
        public Button mButton2;

        public ItemView(TextView text, Button button) {
            mTextView = text;
            mButton1 = button;
            mButton2 = null;
        };

        public ItemView(TextView text, Button button1, Button button2) {
            mTextView = text;
            mButton1 = button1;
            mButton2 = button2;
        };
    }

}